<?php 
namespace app\agent\controller;

use app\common\model\user\Users;
use app\common\model\coin\CoinUser;
use app\common\model\trade\TradeLeveraged;
use think\Db;

class Index extends Base
{
	public function index()
	{
		return view();
	}
	
	public function home()
	{
		$start = strtotime(date('Y-m-d').'00:00:00');
		$end = strtotime(date('Y-m-d').'23:59:59');
		//线下用户总余额
		$user_ids = Users::where(['act_id' => $this->agent_id])->column('id');
		$account = CoinUser::where(['user_id' => ['in',$user_ids],'coin_id' => 5,'state' => 1])->sum('avalible_price');
		//我的保证金
		$bail = Users::where(['id' => $this->agent_id])->value('bail');
		//会员量
		$user_num = count($user_ids);
		//今日注册
		$day_reg = Users::where(['act_id' => $this->agent_id,'create_time' => ['between',[$start,$end]]])->count();
		//今日下单
		
		$day_order = TradeLeveraged::where(['user_id' => ['in',$user_ids],'create_time' => ['between',[$start,$end]],'state' => ['neq',2]])->count();
		//当日提币充币
		$recharge = Db::name('cash_recharge')->where(['user_id' => ['in',$user_ids],'coin_id' => 5,'state' => 1,'create_time' => ['between',[$start,$end]]])->sum('num');
		//获取提现数据
		$cash = Db::name('cash_list')->where(['user_id' => ['in',$user_ids],'coin_id' => 5,'state' => 1,'create_time' => ['between',[$start,$end]]]) -> sum('num');
		
		//总手续费
		$fee = Db::name('trade_leveraged')->where(['user_id' => ['in',$user_ids],'create_time' => ['between',[$start,$end]],'state' => ['neq',2]])->sum('fee');
		//总盈利
		$loss_up_buy = Db::name('trade_leveraged')->where(['user_id' => ['in',$user_ids],'trade_style' => 1,'close' => ['exp',Db::raw('> buy_price')],'create_time' => ['between',[$start,$end]],'state' => ['neq',2]])->sum('real_price');
		$loss_up_sale = Db::name('trade_leveraged')->where(['user_id' => ['in',$user_ids],'trade_style' => 2,'close' => ['exp',Db::raw('< buy_price')],'create_time' => ['between',[$start,$end]],'state' => ['neq',2]])->sum('real_price');
		$loss_up = $loss_up_buy + $loss_up_sale;
		//总亏损
		$loss_down_buy = Db::name('trade_leveraged')->where(['user_id' => ['in',$user_ids],'trade_style' => 1,'close' => ['exp',Db::raw('< buy_price')],'create_time' => ['between',[$start,$end]],'state' => ['neq',2]])->sum('real_price');
		$loss_down_sale = Db::name('trade_leveraged')->where(['user_id' => ['in',$user_ids],'trade_style' => 2,'close' => ['exp',Db::raw('> buy_price')],'create_time' => ['between',[$start,$end]],'state' => ['neq',2]])->sum('real_price');
		$loss_down = $loss_down_buy + $loss_down_sale;
		$this->assign('account',$account);
		$this->assign('bail',$bail);
		$this->assign('user_num',$user_num);
		$this->assign('day_order',$day_order);
		$this->assign('recharge',$recharge);
		$this->assign('cash',$cash);
		$this->assign('fee',$fee);
		$this->assign('loss_up',$loss_up);
		$this->assign('loss_down',$loss_down);
		$this->assign('day_reg',$day_reg);
		return view();
	}
}